class MyTestChild {
  constructor(objFile) {
    process.on("message", async (command) => {
      const __workobj = require(objFile);
      if (command.type == "function") {
        console.log(`this child process is pid ${process.pid}`);
        let rv = await __workobj[command.function](...command.params);
        process.send({ type: "function", function: command.function, callID: command.callID, val: rv });
      } else {
        let rv = await __workobj[command.function](this.callback)
      }

    });
  }

  callback = (...arg) => {
    console.log("in child process callback:", arg);
    process.send({ type: "callback", function: "registCallBack", val: arg });
  }

  getChildObjName = (parentFileName) => {
    let objName = "";
    let charGroup = parentFileName.split("-");
    for (let i = 0; i < charGroup.length - 1; i++) {
      objName += charGroup[i];
      if (i != charGroup.length - 2) {
        objName += "-";
      }
    }

    return objName + "-child.js";
  };

  getObjName = (agentFileName) => {
    let objName = "";
    let charGroup = agentFileName.split("-");
    for (let i = 0; i < charGroup.length - 1; i++) {
      objName += charGroup[i];
      if (i != charGroup.length - 2) {
        objName += "-";
      }
    }

    return objName;
  };
}

const myTestChild = new MyTestChild("./my-test");
module.exports = myTestChild;
